const state = {
    result: [], //用户列表数据
    roles: [], //所有角色
    page: {},
}
const actions = {
    saveUsers: ({ commit }, payload) => {
        commit('SAVEUSERS', payload)
    },
    saveRoles: ({ commit }, payload) => {
        console.log(payload)
        commit('SAVEROLES', payload)
    },
    delUsers: ({ commit }, payload) => {
        commit('DELUSERS', payload)
    },
    updateUser: ({ commit }, payload) => {
        commit('UPDATEUSER', payload)
    },
    add_user: ({ commit }, payload) => {
        commit('ADDUSER', payload)
    }
}
const mutations = {
    SAVEUSERS: (state, payload) => {
        state.result = payload.result
        state.page = {
            size: 10,
            number: payload.number || 1,
            total: payload.resultNum
        }
    },
    SAVEROLES: (state, payload) => state.roles = payload.result,
    DELUSERS: (state, payload) => {
        let users = state.result.content
        let index = users.findIndex(it => it.id === payload)
        users.splice(index, 1)
    },
    UPDATEUSER: (state, payload) => {
        let users = state.result.content
        console.log('payload', payload)
        let index = users.findIndex(it => it.id === payload.result.id)
        console.log('index', index)
        users.splice(index, 1, payload.result)
    },
    ADDUSER: (state, payload) => {
        let users = state.result.content
        users.push(payload.result)
    },
}

const getters = {
    allUsers: state => state.result,
    allRoles: state => state.roles
}
export default {
    state,
    actions,
    mutations,
    getters
}